﻿using Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tools;
using 纺织贸易管理系统.设置窗体;

namespace 纺织贸易管理系统.新增窗体
{
    public partial class 流转登记 : Sunny.UI.UIForm 
    {
        public string Liuzhuankahao { get; set; }
        public FormUseful Useful { get; set; }
        /// <summary>
        /// 日志ID
        /// </summary>
        public int ID { get; set; }
        private List<liuzhuanmingxi> danjumingxis;
        public 流转登记()
        {
            InitializeComponent();
            uiDatetimePicker1 .Value = DateTime.Now;
            uiDatetimePicker2.Value = DateTime.Now;
            cmbjigong.DataSource = YuanGongTableService.GetYuanGongTablelst ().Select(x => x.Xingming).Distinct().ToList();
            cmbjitaihao.DataSource = MachineTableService.GetMachineTablelst ().Select(x => x.MachineID).ToList();
            cmbgongxv.DataSource = TechnologyTableService.GetTechnologyTablelst().Select (x=>x.Technology ).ToList ();         
            cmbcishu.SelectedIndex = 0;
            CreateGrid.CreateDatagridview("流转卡", gridView2);
            gridView2.Columns.Add(cxz);
            //自动补全

        }

        private void 流转登记_Load(object sender, EventArgs e)
        {
            if (Useful == FormUseful.新增)
            {
                try
                {
                    txtliuzhuankaNum.Text = Liuzhuankahao;
                    danjumingxis = liuzhuanmingxiService .Getliuzhuanmingxilst (x => x.CardNum == Liuzhuankahao && x.Employee != string.Empty);
                    cmbgongxv.Text = danjumingxis.Select(x => x.GongyiYaoqiu).ToList().First();
                    txtdanjunum.Text = LiuzhuancardService .GetOneLiuzhuancard (x => x.CardNum == Liuzhuankahao).Shuliang.ToString();
                    txtNum.Text = txtdanjunum.Text;
                    cmbgongxv.Text = liuzhuanlogService.GetOneliuzhuanlog(x => x.CardNum == Liuzhuankahao).GongyiYaoqiu;
                    cmbcishu.SelectedIndex = liuzhuanlogService.Getliuzhuanloglst(x => x.CardNum == Liuzhuankahao).Count;
                    gridView2.DataSource = danjumingxitableService.Getdanjumingxitablelst(x => x.danhao == Liuzhuankahao);
                    foreach(DataGridViewRow row in gridView2.Rows )
                    {
                        row.Cells[cxz.Name].Value = true;
                    }
                }
                catch
                {
                    Sunny.UI.UIMessageBox.ShowError("没有找到相关信息！");
                }
            }
            else
            {
                if (Useful == FormUseful.修改)
                {
                    var liuzhuanka = liuzhuanlogService.GetOneliuzhuanlog(x => x.ID == ID);
                    txtbeizhu.Text = liuzhuanka.Beizhu;
                    txtdanjunum.Text = LiuzhuancardService.GetOneLiuzhuancard(x => x.CardNum == liuzhuanka.CardNum).Shuliang.ToString();
                    cmbjitaihao.Text = liuzhuanka.MachineID;
                    cmbjigong.Text = liuzhuanka.Employee;
                    cmbgongxv.Text = liuzhuanka.GongyiYaoqiu;
                    cmbcishu.Text = liuzhuanka.Cishu.ToString();
                    uiDatetimePicker1.Value = liuzhuanka._date;
                    txtNum.Text = liuzhuanka.Num.ToString();
                    txtliuzhuankaNum.Text = liuzhuanka.CardNum;
                    txtCheShu.Text = liuzhuanka.Cheshu.ToString();
                    txtfangrongdian.Text = liuzhuanka.FangRongDian;
                    txtWendu.Text = liuzhuanka.WengDu.ToString();
                    uiDatetimePicker2.Value = liuzhuanka.ShangXiaJi;
                    var log = liuzhuanlogService.GetOneliuzhuanlog(x => x.ID == ID);
                    string danhao = log.CardNum + log._date.ToString();
                    var mingxi = danjumingxitableService.Getdanjumingxitablelst(x => x.danhao == danhao);
                    var danjumingxis = danjumingxitableService.Getdanjumingxitablelst(x => x.danhao == liuzhuanka.CardNum);
                    gridView2.DataSource = danjumingxis;
                    foreach (DataGridViewRow row in gridView2.Rows)
                    {
                        row.Cells[cxz.Name].Value = mingxi.Where  (x => x.ganghao == danjumingxis[row.Index ].ganghao ).ToList ().Count >0?true :false ;
                    }
                }
            }
        }

        private void cmbjigong_TextChanged(object sender, EventArgs e)
        {
            //if (danjumingxis != null)
            //{
            //    cmbgongxv.DataSource = danjumingxis.Where(x => x.Employee == cmbjigong.Text).ToList();
            //    cmbgongxv.DisplayMember = "GongyiYaoqiu";
            //}
        }

        private void cmbgongxv_TextChanged(object sender, EventArgs e)
        {
            if (danjumingxis != null)
            {
                if (danjumingxis.Where(x => x.GongyiYaoqiu == cmbgongxv.Text).ToList().Count > 0)
                {
                    var d = danjumingxis.First(x => x.GongyiYaoqiu == cmbgongxv.Text);
                    cmbcishu.Text = d.Cishu.ToString();
                    cmbjigong.Text = d.Employee;
                    cmbjitaihao.Text = d.MachineID;
                }
            }
        }

        private void uiSymbolButton1_Click(object sender, EventArgs e)
        {
            gridView2.EndEdit();
            if(string.IsNullOrWhiteSpace ( cmbjigong.Text))
            {
                ShowMessageBox.ShowErrorBox ( "请填写或者选择操作员");
                return;
            }
            if (string.IsNullOrWhiteSpace(txtliuzhuankaNum .Text))
            {
                ShowMessageBox.ShowErrorBox (  "请填写流转卡号");
                return;
            }
            if ( LiuzhuancardService.GetLiuzhuancardlst (x=>x.CardNum ==txtliuzhuankaNum.Text).Count ==0)
            {
                ShowMessageBox.ShowErrorBox (  "未找到该流转卡的相关信息！请确认流转卡号是否填写正确");
                return;
            }
            if (string.IsNullOrWhiteSpace(cmbjitaihao .Text))
            {
                ShowMessageBox.ShowErrorBox (  "请填写或者选择机台号");
                return;
            }
            var liuzhuanlog = new liuzhuanlog()
            {
                Cishu = cmbcishu.Text,
                GongyiYaoqiu = cmbgongxv.Text,
                Beizhu = txtbeizhu.Text,
                CardNum = txtliuzhuankaNum.Text,
                Employee = cmbjigong.Text,
                MachineID = cmbjitaihao.Text,
                Num = txtNum.Text.ToDecimal (),
                _date = uiDatetimePicker1.Value,
                Cheshu=txtCheShu.Text,
                WengDu=txtWendu.Text,
                ShangXiaJi =uiDatetimePicker2.Value ,
                FangRongDian =txtfangrongdian.Text,
                Own=Tools.CurrentUser.user.YHBH 
        };
            if (Useful == FormUseful .新增)
            {
               liuzhuanlogService .Insertliuzhuanlog (liuzhuanlog);
                List<danjumingxitable> mingxi = gridView2.DataSource as List<danjumingxitable>;
                foreach (DataGridViewRow row in gridView2.Rows)
                {
                    if (row.Cells[cxz.Name].Value != null && (Boolean)row.Cells[cxz.Name].Value)
                    {
                        mingxi[row.Index].danhao = txtliuzhuankaNum.Text + uiDatetimePicker1.Value.ToString();
                        danjumingxitableService.Insertdanjumingxitable(mingxi[row.Index]);
                    }
                }             
            }
            else
            {              
                var log = liuzhuanlogService.GetOneliuzhuanlog(x => x.ID == ID);
                string danhao = log.CardNum + log._date.ToString();
                danjumingxitableService.Deletedanjumingxitable(x => x.danhao == danhao);
                liuzhuanlogService .Updateliuzhuanlog (liuzhuanlog,x=>x.ID==ID);
                List<danjumingxitable> mingxi = gridView2.DataSource as List<danjumingxitable>;
                foreach (DataGridViewRow row in gridView2.Rows)
                {
                    if (row.Cells[cxz.Name].Value != null && (Boolean)row.Cells[cxz.Name].Value)
                    {
                        mingxi[row.Index].danhao = txtliuzhuankaNum.Text + uiDatetimePicker1.Value.ToString();
                        danjumingxitableService.Insertdanjumingxitable(mingxi[row.Index]);
                    }
                }
            }
            UINotifier.Show("保存成功", UINotifierType.OK, "后整理管理系统", false, 2000, null);
            Useful = FormUseful.新增;
            cmbjitaihao.Text = "";
            cmbjigong.Text = "";
            cmbcishu.SelectedIndex++;
            Thread.Sleep(1000);
            uiDatetimePicker1.Value = DateTime.Now;
            foreach (DataGridViewRow row in gridView2.Rows)
            {
                row.Cells[cxz.Name].Value = true;
            }
            gridView2.EndEdit();
        }

        private void uiSymbolButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtliuzhuankaNum_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                danjumingxis = liuzhuanmingxiService.Getliuzhuanmingxilst(x => x.CardNum == txtliuzhuankaNum.Text  && x.Employee != string.Empty);
                var liuzhuanka = LiuzhuancardService.GetOneLiuzhuancard(x => x.CardNum == txtliuzhuankaNum.Text );
                txtNum.Text = txtdanjunum.Text = liuzhuanka.Shuliang.ToString();
                cmbgongxv.Text = liuzhuanlogService.GetOneliuzhuanlog(x => x.CardNum == txtliuzhuankaNum.Text ).GongyiYaoqiu;
                cmbcishu.SelectedIndex = liuzhuanlogService.Getliuzhuanloglst(x => x.CardNum == txtliuzhuankaNum.Text ).Count;
                gridView2.DataSource = danjumingxitableService.Getdanjumingxitablelst(x => x.danhao == txtliuzhuankaNum.Text);
            }
        }

        private void uiDatetimePicker2_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode==Keys.Enter||e.KeyCode==Keys.Down  )
            {
                SendKeys.SendWait("{Tab}");
            }
        }

        private void txtCheShu_Enter(object sender, EventArgs e)
        {
            BeginInvoke((Action)delegate
            {
                UITextBox t = (UITextBox)sender;
                t.SelectAll();
            });
        }

        private void txtliuzhuankaNum_Enter(object sender, EventArgs e)
        {
            BeginInvoke((Action)delegate
            {
                UITextBox t = (UITextBox)sender;
                t.Select(5, t.Text.Length - 5);
            });
           
        }

        private void gridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            decimal Num = 0;
            List<danjumingxitable> mingxi = gridView2.DataSource as List<danjumingxitable>;
            foreach (DataGridViewRow row in gridView2.Rows)
            {
                if (row.Cells[cxz.Name].Value != null && (Boolean)row.Cells[cxz.Name].Value)
                {
                    Num += mingxi[row.Index].chengpingmishu;
                }
            }
            txtNum.Text = Num.ToString();
        }
    }
}
